﻿using System.Data;
using System.Data.SqlClient;
using MvcApplication2.Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace MvcApplication2.Models.Services
{
    public class NguoiDungService
    {
        public static string Register(NguoiDung nguoiDung)
        {
            MyDataProvider.Open();
            var sqlParameterList = new List<SqlParameter>();
            sqlParameterList.Add(new SqlParameter("@Username",nguoiDung.Username));
            sqlParameterList.Add(new SqlParameter("@Password",nguoiDung.Password));
            sqlParameterList.Add(new SqlParameter("@TenNguoiDung",nguoiDung.TenNguoiDung));
            sqlParameterList.Add(new SqlParameter("@NgaySinh",nguoiDung.NgaySinh));
            sqlParameterList.Add(new SqlParameter("@Email",nguoiDung.Email));
            sqlParameterList.Add(new SqlParameter("@DiaChi",nguoiDung.DiaChi));
            sqlParameterList.Add(new SqlParameter("@DienThoai",nguoiDung.DienThoai??string.Empty));
            sqlParameterList.Add(new SqlParameter("@IDLoaiNguoiDung",DummyEnum.LoaiNguoiDungEnum.KhachHang.GetHashCode()));
            sqlParameterList.Add(new SqlParameter("@DiemThuong",SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = 0;
            sqlParameterList.Add(new SqlParameter("@IDDaiLy",SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = DBNull.Value;
            try
            {
                MyDataProvider.insertQueryWithParameters("spu_dangkytaikhoan", sqlParameterList);
            }
            catch (SqlException exception)
            {
                MyDataProvider.Close();
                return exception.Message;
            }
            MyDataProvider.Close();
            return "Đăng ký thành công";
        }

        public static Tuple<string,NguoiDung> SignIn(String username, String password, bool createCookie)
        {
            MyDataProvider.Open();
            var sqlParameterList = new List<SqlParameter>();
            sqlParameterList.Add(new SqlParameter("@Username",username));
            sqlParameterList.Add(new SqlParameter("@Password",password));
            var nguoiDungList =
                Utility.ToList<NguoiDung>(MyDataProvider.excuteQueryWithParameters("spu_nguoidung_signIn",
                                                                                   sqlParameterList));
            MyDataProvider.Close();
            if (nguoiDungList.Count > 0)
            {
                FormsAuthentication.SetAuthCookie(username, createCookie);
                return new Tuple<string, NguoiDung>(null,nguoiDungList[0]);
            }
            else
            {
                return new Tuple<string, NguoiDung>("Tên đăng nhập hoặc mật khẩu không chính xác",null);
            }
        }

        public static NguoiDung GetByUsername(string username)
        {
            MyDataProvider.Open();
            var sqlParameterList = new List<SqlParameter>();
            sqlParameterList.Add(new SqlParameter("@Username", username));
            var nguoiDung = Utility.ToList<NguoiDung>(MyDataProvider.excuteQueryWithParameters("spu_nguoidung_getByUsername", sqlParameterList))[0];
            MyDataProvider.Close();
            return nguoiDung;
        }
    }
}